package me.zhengjie.suli.dashboard.repository.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.suli.dashboard.repository.DashboardRepository;
import me.zhengjie.suli.wcs.domain.AgvTasks;
import me.zhengjie.suli.wcs.repository.AgvTasksRepository;
import me.zhengjie.util.CommonUtil;
import me.zhengjie.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DashboardRepositoryImpl implements DashboardRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private AgvTasksRepository agvTasksRepository;
    @Autowired
    private CommonUtil commonUtil;

    @Override
    public Map<String, List<Integer>> getDataByType(String type) {
        String sql = "SELECT COUNT(TaskNo) as taskNums, CONVERT ( VARCHAR, create_time, 111 ) as createTime " +
                "FROM stacker_task " +
                "WHERE TaskType = ? " +
                "AND DATEDIFF( dd, create_time, GETDATE( ) ) < 7 " +
                "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) " +
                "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) asc";
        List<Map<String, Object>> inHouseList = jdbcTemplate.queryForList(sql, 1);
        List<Map<String, Object>> outHouseList = jdbcTemplate.queryForList(sql, 2);
        List<Map<String, Object>> moveList = jdbcTemplate.queryForList(sql, 3);
        List<Map<String, Object>> checkList = jdbcTemplate.queryForList(sql, 5);

        List<Integer> inHouseResultList = new ArrayList<>();
        List<Integer> outHouseResultList = new ArrayList<>();
        List<Integer> moveHouseResultList = new ArrayList<>();
        List<Integer> checkHouseResultList = new ArrayList<>();

        List<String> dateList = new ArrayList<>();
        for (int k = 6; k >= 0; k--) {
            // 所有的时间列表，有序
            dateList.add(DateUtil.getDaysBefore(k));
        }

        int inHouseFlag = 0; // 标记位
        int outHouseFlag = 0; // 标记位
        int moveFlag = 0; // 标记位
        int checkFlag = 0; // 标记位

        for (int i = 0; i < dateList.size(); i++) {
            inHouseFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < inHouseList.size(); k++) {
                String create_time = inHouseList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(inHouseList.get(k).get("taskNums").toString());
                    inHouseResultList.add(count);
                    inHouseFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (inHouseFlag == 0) {
                inHouseResultList.add(0);
            }
        }

        for (int i = 0; i < dateList.size(); i++) {
            outHouseFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < outHouseList.size(); k++) {
                String create_time = outHouseList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(outHouseList.get(k).get("taskNums").toString());
                    outHouseResultList.add(count);
                    outHouseFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (outHouseFlag == 0) {
                outHouseResultList.add(0);
            }
        }

        for (int i = 0; i < dateList.size(); i++) {
            moveFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < moveList.size(); k++) {
                String create_time = moveList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(moveList.get(k).get("taskNums").toString());
                    moveHouseResultList.add(count);
                    moveFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (moveFlag == 0) {
                moveHouseResultList.add(0);
            }
        }

        for (int i = 0; i < dateList.size(); i++) {
            checkFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < checkList.size(); k++) {
                String create_time = checkList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(checkList.get(k).get("taskNums").toString());
                    checkHouseResultList.add(count);
                    checkFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (checkFlag == 0) {
                checkHouseResultList.add(0);
            }
        }

        Map<String, List<Integer>> resultList = new HashMap<>();
        resultList.put("inHouse", inHouseResultList);
        resultList.put("outHouse", outHouseResultList);
        resultList.put("move", moveHouseResultList);
        resultList.put("check", checkHouseResultList);
        return resultList;
    }

    @Override
    public Map<String, Integer> getPieChartData() {
        String sql = "select COUNT(TaskNo) as 'taskNum', TaskType FROM stacker_task GROUP BY TaskType";
        Map<String, Integer> resultMap = new HashMap<>();
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : dataList) {
            if (map.get("taskType").toString().equals("1")) {
                resultMap.put("inHouse", Integer.parseInt(map.get("taskNum").toString()));
            } else if (map.get("taskType").toString().equals("2")) {
                resultMap.put("outHouse", Integer.parseInt(map.get("taskNum").toString()));
            } else if (map.get("taskType").toString().equals("3")) {
                resultMap.put("move", Integer.parseInt(map.get("taskNum").toString()));
            } else if (map.get("taskType").toString().equals("5")) {
                resultMap.put("check", Integer.parseInt(map.get("taskNum").toString()));
            }
        }

        if (resultMap.get("inHouse") == null) {
            resultMap.put("inHouse", 0);
        }
        if (resultMap.get("outHouse") == null) {
            resultMap.put("outHouse", 0);
        }
        if (resultMap.get("move") == null) {
            resultMap.put("move", 0);
        }
        if (resultMap.get("check") == null) {
            resultMap.put("check", 0);
        }

        return resultMap;
    }

    @Override
    public Map<String, List<Integer>> getBarchartData() {
        String sql = "SELECT COUNT( task_no ) AS taskNums, CONVERT ( VARCHAR, create_time, 111 ) AS createTime"
                + " FROM error_record "
                + "WHERE DATEDIFF( dd, create_time, GETDATE( ) ) < 7 "
                + "AND task_type = 1 "
                + "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) "
                + "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) ASC";
        List<Map<String, Object>> errorRecordList = jdbcTemplate.queryForList(sql);

        String sql2 = "SELECT COUNT(TaskNo) as taskNums, CONVERT ( VARCHAR, create_time, 111 ) as createTime " +
                "FROM stacker_task WHERE " +
                "DATEDIFF( dd, create_time, GETDATE( ) ) < 7 " +
                "AND TaskType = 1 " +
                "AND State = 2 " +
                "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) " +
                "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) asc";
        List<Map<String, Object>> stackTaskList = jdbcTemplate.queryForList(sql2);

        Map<String, List<Integer>> resultMap = new HashMap<>();

        List<String> dateList = new ArrayList<>();
        for (int k = 6; k >= 0; k--) {
            // 所有的时间列表，有序
            dateList.add(DateUtil.getDaysBefore(k));
        }
        int errorRecordFlag = 0;
        int stackerTaskFlag = 0;

        List<Integer> errorRecordResultList = new ArrayList<>();
        List<Integer> stackerTasksResultList = new ArrayList<>();

        for (int i = 0; i < dateList.size(); i++) {
            errorRecordFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < errorRecordList.size(); k++) {
                String create_time = errorRecordList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(errorRecordList.get(k).get("taskNums").toString());
                    errorRecordResultList.add(count);
                    errorRecordFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (errorRecordFlag == 0) {
                errorRecordResultList.add(0);
            }
        }

        for (int i = 0; i < dateList.size(); i++) {
            stackerTaskFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < stackTaskList.size(); k++) {
                String create_time = stackTaskList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(stackTaskList.get(k).get("taskNums").toString());
                    stackerTasksResultList.add(count);
                    stackerTaskFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (stackerTaskFlag == 0) {
                stackerTasksResultList.add(0);
            }
        }
        Map<String, List<Integer>> resultList = new HashMap<>();
        resultList.put("errorRecord", errorRecordResultList);
        resultList.put("stackerTask", stackerTasksResultList);
        return resultList;

    }

    @Override
    public Map<String, List<Integer>> getBarchartDataOutHouse() {
        String sql = "SELECT COUNT( task_no ) AS taskNums, CONVERT ( VARCHAR, create_time, 111 ) AS createTime"
                + " FROM error_record "
                + "WHERE DATEDIFF( dd, create_time, GETDATE( ) ) < 7 "
                + "AND task_type = 2 "
                + "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) "
                + "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) ASC";
        List<Map<String, Object>> errorRecordList = jdbcTemplate.queryForList(sql);

        String sql2 = "SELECT COUNT(TaskNo) as taskNums, CONVERT ( VARCHAR, create_time, 111 ) as createTime " +
                "FROM stacker_task WHERE " +
                "DATEDIFF( dd, create_time, GETDATE( ) ) < 7 " +
                "AND TaskType = 2 " +
                "AND State = 2 " +
                "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) " +
                "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) asc";
        List<Map<String, Object>> stackTaskList = jdbcTemplate.queryForList(sql2);

        Map<String, List<Integer>> resultMap = new HashMap<>();

        List<String> dateList = new ArrayList<>();
        for (int k = 6; k >= 0; k--) {
            // 所有的时间列表，有序
            dateList.add(DateUtil.getDaysBefore(k));
        }
        int errorRecordFlag = 0;
        int stackerTaskFlag = 0;

        List<Integer> errorRecordResultList = new ArrayList<>();
        List<Integer> stackerTasksResultList = new ArrayList<>();

        for (int i = 0; i < dateList.size(); i++) {
            errorRecordFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < errorRecordList.size(); k++) {
                String create_time = errorRecordList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(errorRecordList.get(k).get("taskNums").toString());
                    errorRecordResultList.add(count);
                    errorRecordFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (errorRecordFlag == 0) {
                errorRecordResultList.add(0);
            }
        }

        for (int i = 0; i < dateList.size(); i++) {
            stackerTaskFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < stackTaskList.size(); k++) {
                String create_time = stackTaskList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(stackTaskList.get(k).get("taskNums").toString());
                    stackerTasksResultList.add(count);
                    stackerTaskFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (stackerTaskFlag == 0) {
                stackerTasksResultList.add(0);
            }
        }
        Map<String, List<Integer>> resultList = new HashMap<>();
        resultList.put("errorRecord", errorRecordResultList);
        resultList.put("stackerTask", stackerTasksResultList);
        return resultList;
    }

    @Override
    public Map<String, Integer[]> getTimeNumber() {
        Map<String, Integer[]> resultMap = new HashMap<>();
        /** 当日各时段已完成【出库】数量 */
        String sql = "SELECT COUNT (task_code ) AS nums, DATEPART (hh, start_time) AS createTime FROM tasks " +
                "where task_status = 2 and task_type = 2 and DateDiff(dd,start_time,getDate())=0 " +
                "GROUP BY DATEPART (hh, start_time) " +
                "ORDER BY createTime asc ";
        Integer[] integers = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        List<Map<String, Object>> outHouseList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : outHouseList) {
            int hour = (int) map.get("createTime");
            integers[hour] = (Integer) map.get("nums");
        }
        resultMap.put("outHouseData", integers);

        /** 当日各时段已完成【入库】数量 */
        String sql2 = "SELECT COUNT (task_code ) AS nums, DATEPART (hh, start_time) AS createTime FROM tasks " +
                "where task_status = 2 and task_type = 1 and DateDiff(dd,start_time,getDate())=0 " +
                "GROUP BY DATEPART (hh, start_time) " +
                "ORDER BY createTime asc ";
        Integer[] integers2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        List<Map<String, Object>> inHouseList = jdbcTemplate.queryForList(sql2);
        for (Map<String, Object> map : inHouseList) {
            int hour = (int) map.get("createTime");
            integers2[hour] = (Integer) map.get("nums");
        }
        resultMap.put("inHouseData", integers2);
        return resultMap;
    }

    @Override
    public Map<String, Integer> getBarchartLocationData() {
        String sql = "select COUNT(id) as [num], [status] from location GROUP BY [status]";
        List<Map<String, Object>> locationList = jdbcTemplate.queryForList(sql);
        Map<String, Integer> resultMap = new HashMap<>();

        for (Map<String, Object> map : locationList) {
            if (map.get("status").toString().equals("0")) {
                resultMap.put("canUse", Integer.parseInt(map.get("num").toString()));
            } else if (map.get("status").toString().equals("1")) {
                resultMap.put("isUsed", Integer.parseInt(map.get("num").toString()));
            } else if (map.get("status").toString().equals("2")) {
                resultMap.put("banned", Integer.parseInt(map.get("num").toString()));
            }
        }

        if (resultMap.get("canUse") == null) {
            resultMap.put("canUse", 0);
        }
        if (resultMap.get("isUsed") == null) {
            resultMap.put("isUsed", 0);
        }
        if (resultMap.get("banned") == null) {
            resultMap.put("banned", 0);
        }

        return resultMap;
    }

    @Override
    public Map<String, Integer> getTodayNumber() {
        Map<String, Integer> result = new HashMap<>();

        /** 本周总订单数 */
        String sql5 = "select count(task_code) as value5 from tasks " +
                "where DateDiff(dd,start_time,getDate()) < 7";
        try {
            Map<String, Object> map5 = jdbcTemplate.queryForMap(sql5);
            result.put("value5", (Integer) map5.get("value5"));
        } catch (DataAccessException e) {
            result.put("value5", 0);
        }

        /** 本周待处理订单数 */
        String sql6 = "select count(task_code) as value6 from tasks " +
                "where status !=  2 AND task_type in (1,2)"
                + "and DateDiff(dd,create_time,getDate()) < 7 ";
        try {
            Map<String, Object> map6 = jdbcTemplate.queryForMap(sql6);
            result.put("value6", (Integer) map6.get("value6"));
        } catch (DataAccessException e) {
            result.put("value6", 0);
        }

        /** 待入库 */
        String sql = "select count(task_code) as value2 from tasks " +
                "where task_type = 1 " +
                "and status != 2 " +
                "and DateDiff(dd,create_time,getDate()) < 7 ";

        try {
            Map<String, Object> map1 = jdbcTemplate.queryForMap(sql);
            result.put("value2", (Integer) map1.get("value2"));
        } catch (DataAccessException e) {
//            result.put("value2", new Random().nextInt(20));
            result.put("value2", 0);
        }


        /** 本周已入库 */
        String sql2 = "select count(task_code) as value1 from tasks " +
                "where task_type = 1 " +
                "and status = 2 " +
                "and DateDiff(dd,end_time,getDate()) < 7 " +
                "group by task_type ";
        try {
            Map<String, Object> map2 = jdbcTemplate.queryForMap(sql2);
            result.put("value1", (Integer) map2.get("value1"));
        } catch (DataAccessException e) {
//            result.put("value1", new Random().nextInt(20));
            result.put("value1", 0);
        }


        /** 待出库 */
        String sql3 = "select count(task_code) as value4 from tasks " +
                "where task_type = 2 " +
                "and status != 2 and DateDiff(dd,create_time,getDate()) < 7" +
                "group by task_type ";

        try {
            Map<String, Object> map3 = jdbcTemplate.queryForMap(sql3);
            result.put("value4", (Integer) map3.get("value4"));
        } catch (DataAccessException e) {
//            result.put("value4", new Random().nextInt(20));
            result.put("value4", 0);
        }

        /** 本周已出库 */
        String sql4 = "select count(task_code) as value3 from tasks " +
                "where task_type = 2 " +
                "and status = 2 " +
                "and DateDiff(dd,create_time,getDate()) < 7 " +
                "group by task_type ";
        try {
            Map<String, Object> map4 = jdbcTemplate.queryForMap(sql4);
            result.put("value3", (Integer) map4.get("value3"));
        } catch (DataAccessException e) {
//            result.put("value3", new Random().nextInt(20));
            result.put("value3", 0);
        }

        /** 剩余库位数 */
        String sql7 = "select count(id) as value7 from location where status in (0,3) ";
        try {
            Map<String, Object> map5 = jdbcTemplate.queryForMap(sql7);
            result.put("value7", (Integer) map5.get("value7"));
        } catch (DataAccessException e) {
            result.put("value7", 0);
        }

        /** 占用库位数 */
        String sql8 = "select count(id) as value8 from location where status in (1,2) ";
        try {
            Map<String, Object> map8 = jdbcTemplate.queryForMap(sql8);
            result.put("value8", (Integer) map8.get("value8"));
        } catch (DataAccessException e) {
            result.put("value8", 0);
        }

        try {
            try{
                List<String> list = new ArrayList<>();
                list.add("kubot-1");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("robotCodes",list);
                String s = commonUtil.httpPost("http://10.198.76.25:9046/robot/query", JSON.toJSONString(jsonObject));
                JSONObject jsonObject1 = JSON.parseObject(s);
                JSONObject data = jsonObject1.getJSONObject("data");
                JSONArray jsonObjectList = data.getJSONArray("robots");
                JSONObject jsonObject2 = jsonObjectList.getJSONObject(0);
                String workState = jsonObject2.getString("workState");
                if (workState.equals("WORKING")) {
                    result.put("srm1", 1);  // 运行中
                } else if (workState.equals("OTHER")){
                    result.put("srm1", 0);  // 空闲
                } else if (workState.equals("OFFLINE")){
                    result.put("srm1", 2);  // 下线
                } else if (workState.equals("WORKING")){
                    result.put("srm1", 3);  // 充电
                } else if (workState.equals("EXCEPTION")){
                    result.put("srm1", 4);  // 异常
                }
            }catch (Exception e){
                result.put("srm1", 5);
            }
            try{
                List<String> list1 = new ArrayList<>();
                list1.add("kubot-2");
                JSONObject js = new JSONObject();
                js.put("robotCodes",list1);
                String s1 = commonUtil.httpPost("http://10.198.76.25:9046/robot/query", JSON.toJSONString(js));
                JSONObject jsonObject11 = JSON.parseObject(s1);
                JSONObject data1 = jsonObject11.getJSONObject("data");
                JSONArray jsonObjectList1 = data1.getJSONArray("robots");
                JSONObject jsonObject21 = jsonObjectList1.getJSONObject(0);
                String workState1 = jsonObject21.getString("workState");
                if (workState1.equals("WORKING")) {
                    result.put("srm2", 1);  // 运行中
                } else if (workState1.equals("OTHER")){
                    result.put("srm2", 0);  // 空闲
                } else if (workState1.equals("OFFLINE")){
                    result.put("srm2", 2);  // 下线
                } else if (workState1.equals("WORKING")){
                    result.put("srm2", 3);  // 充电
                } else if (workState1.equals("EXCEPTION")){
                    result.put("srm2", 4);  // 异常
                }
            }catch (Exception e){
                result.put("srm2", 5);
            }

//            if (rgvTasks != null) {
//                result.put("rgv", 1); // 运行中
//            } else {
//                result.put("rgv", 0); // 空闲
//            }
//            if (agvTasks != null) {
//                result.put("agv", 1); // 运行中
//            } else {
//                result.put("agv", 0); // 空闲
//            }
//            result.put("tf1", 1);
//            result.put("tf2", 1);

        } catch (DataAccessException e) {
            result.put("srm1", 0);  // 空闲
            result.put("srm2", 0);  // 空闲
            result.put("rgv", 0); // 空闲
            result.put("agv", 0); // 空闲
        }


        return result;
    }

    @Override
    public Map<String, int[]> getOneWeekToAndOutData() {
        String sql1 = "SELECT COUNT(TaskNo) as taskNums, CONVERT ( VARCHAR, create_time, 111 ) as createTime " +
                "FROM stacker_task WHERE " +
                "DATEDIFF( dd, create_time, GETDATE( ) ) < 7 " +
                "AND TaskType = 1 " +
                "AND State = 2 " +
                "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) " +
                "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) asc";
        List<Map<String, Object>> stackTaskList = jdbcTemplate.queryForList(sql1);
        List<String> dateList = new ArrayList<>();
        for (int k = 6; k >= 0; k--) {
            // 所有的时间列表，有序
            dateList.add(DateUtil.getDaysBefore(k));
        }

        int stackerTaskFlag = 0;
        int[] stackerTasksResultList = new int[7];
        for (int i = 0; i < dateList.size(); i++) {
            stackerTaskFlag = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < stackTaskList.size(); k++) {
                String create_time = stackTaskList.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(stackTaskList.get(k).get("taskNums").toString());
                    stackerTasksResultList[i] = count;
                    stackerTaskFlag = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (stackerTaskFlag == 0) {
                stackerTasksResultList[i] = 0;
            }
        }
        Map<String, int[]> resultList = new HashMap<>();
        resultList.put("toHouse", stackerTasksResultList);


        String sql2 = "SELECT COUNT(TaskNo) as taskNums, CONVERT ( VARCHAR, create_time, 111 ) as createTime " +
                "FROM stacker_task WHERE " +
                "DATEDIFF( dd, create_time, GETDATE( ) ) < 7 " +
                "AND TaskType = 2 " +
                "AND State = 2 " +
                "GROUP BY CONVERT ( VARCHAR, create_time, 111 ) " +
                "ORDER BY CONVERT ( VARCHAR, create_time, 111 ) asc";
        List<Map<String, Object>> stackTaskListOut = jdbcTemplate.queryForList(sql2);
        int stackerTaskFlagOut = 0;
        int[] stackerTasksResultListOut = new int[7];
        for (int i = 0; i < dateList.size(); i++) {
            stackerTaskFlagOut = 0;
            String date1 = dateList.get(i);
            for (int k = 0; k < stackTaskListOut.size(); k++) {
                String create_time = stackTaskListOut.get(k).get("createTime").toString();
                // 如果在结果队列中发现了一样的日期，则代表可以往结果数组中添加值。并且跳出当前循环
                if (date1.equals(create_time)) {
                    Integer count = Integer.parseInt(stackTaskListOut.get(k).get("taskNums").toString());
                    stackerTasksResultListOut[i] = count;
                    stackerTaskFlagOut = 1; // i 对应的时间已经插入过值了
                    break;
                }
            }
            if (stackerTaskFlagOut == 0) {
                stackerTasksResultListOut[i] = 0;
            }
        }
        resultList.put("outHouse", stackerTasksResultListOut);
        return resultList;
    }

    @Override
    public List<Map<String, Object>> getTasks() {
        String sql = "SELECT * FROM ( " +
                "SELECT task_code, label as 'task_type', td.lot_code, td.qty, material_name, t.create_time FROM " +
                "tasks t, " +
                "task_detail td, " +
                "sys_dict sd, " +
                "sys_dict_detail sdd " +
                "WHERE " +
                "sd.dict_id = sdd.dict_id " +
                "AND t.id = td.task_id " +
                "AND sd.dict_id = 12 " +
                "AND sdd.[value] = t.task_type " +
                ") AS c1, " +
                "( SELECT  task_code as 'task_no', label as 'task_status' " +
                "FROM sys_dict_detail, tasks " +
                "WHERE dict_id = 13 AND tasks.task_status = sys_dict_detail.value ) AS c2 " +
                "where c1.task_code = c2.task_no " +
                "order by c1.create_time desc";
        List<Map<String, Object>> tasksList = jdbcTemplate.queryForList(sql);
        return tasksList;
    }
}
